//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AveragingRatePricer.h"
using namespace Cephei::QL::Experimental::Coupons;
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Experimental/Coupons/SubPeriodsPricer.h>
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Coupons::CAveragingRatePricer::CAveragingRatePricer (boost::shared_ptr<QuantLib::AveragingRatePricer>& childNative, Object^ owner) : CSubPeriodsPricer(CAveragingRatePricer::typeid)
{
#ifdef HANDLE
	_phAveragingRatePricer = NULL;
#endif
	_ppAveragingRatePricer = &childNative;
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppAveragingRatePricer));
}
Cephei::QL::Experimental::Coupons::CAveragingRatePricer::CAveragingRatePricer (QuantLib::AveragingRatePricer& childNative, Object^ owner) : CSubPeriodsPricer(CAveragingRatePricer::typeid)
{
#ifdef HANDLE
	_phAveragingRatePricer = NULL;
#endif
	_ppAveragingRatePricer = new boost::shared_ptr<QuantLib::AveragingRatePricer> (&childNative);
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppAveragingRatePricer));
    _AveragingRatePricerOwner = owner;
    _SubPeriodsPricerOwner = owner;
}

Cephei::QL::Experimental::Coupons::CAveragingRatePricer::CAveragingRatePricer (CAveragingRatePricer^ copy) : CSubPeriodsPricer(CAveragingRatePricer::typeid)
{
#ifdef HANDLE
	_phAveragingRatePricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAveragingRatePricer = new boost::shared_ptr<QuantLib::AveragingRatePricer> (copy->GetShared());
        _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppAveragingRatePricer));
    }
}
Cephei::QL::Experimental::Coupons::CAveragingRatePricer::CAveragingRatePricer (PLATFORM::Type^ t) : CSubPeriodsPricer(CAveragingRatePricer::typeid)
{
#ifdef HANDLE
	_phAveragingRatePricer = NULL;
#endif
	if (!t->IsSubclassOf(CAveragingRatePricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Coupons::CAveragingRatePricer::CAveragingRatePricer (QuantLib::Handle<QuantLib::AveragingRatePricer>& childNative, Object^ owner)  : CSubPeriodsPricer(CAveragingRatePricer::typeid)
{
	_phAveragingRatePricer = &childNative;
	_ppAveragingRatePricer = &static_cast<boost::shared_ptr<QuantLib::AveragingRatePricer>>(childNative.currentLink());
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppAveragingRatePricer));
    _AveragingRatePricerOwner = owner;
}
Cephei::QL::Experimental::Coupons::CAveragingRatePricer::CAveragingRatePricer (QuantLib::Handle<QuantLib::AveragingRatePricer> childNative)  : CSubPeriodsPricer(CAveragingRatePricer::typeid)
{
	_phAveragingRatePricer = &childNative;
	_ppAveragingRatePricer = &static_cast<boost::shared_ptr<QuantLib::AveragingRatePricer>>(childNative.currentLink());
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppAveragingRatePricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Coupons::CAveragingRatePricer::CAveragingRatePricer (QuantLib::AveragingRatePricer childNative)  : CSubPeriodsPricer(CAveragingRatePricer::typeid)
{
#ifdef HANDLE
	_phAveragingRatePricer = NULL;
#endif
	_ppAveragingRatePricer = new boost::shared_ptr<QuantLib::AveragingRatePricer> (new QuantLib::AveragingRatePricer (childNative));
    _ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (boost::dynamic_pointer_cast<QuantLib::SubPeriodsPricer> (*_ppAveragingRatePricer));
}
#endif

Cephei::QL::Experimental::Coupons::CAveragingRatePricer::~CAveragingRatePricer ()
{
    if (_ppAveragingRatePricer != NULL)
    {
	    delete _ppAveragingRatePricer;
        _ppAveragingRatePricer = NULL;
    }
}
Cephei::QL::Experimental::Coupons::CAveragingRatePricer::!CAveragingRatePricer ()
{
    if (_ppAveragingRatePricer != NULL)
    {
	    delete _ppAveragingRatePricer;
    }
}
QuantLib::AveragingRatePricer& Cephei::QL::Experimental::Coupons::CAveragingRatePricer::GetReference ()
{
    if (_ppAveragingRatePricer == NULL) throw REFNEW NativeNullException ();
	return **_ppAveragingRatePricer;
}
boost::shared_ptr<QuantLib::AveragingRatePricer>& Cephei::QL::Experimental::Coupons::CAveragingRatePricer::GetShared ()
{
    if (_ppAveragingRatePricer == NULL) throw REFNEW NativeNullException ();
	return *_ppAveragingRatePricer;
}
QuantLib::AveragingRatePricer* Cephei::QL::Experimental::Coupons::CAveragingRatePricer::GetPointer ()
{
    if (_ppAveragingRatePricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppAveragingRatePricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AveragingRatePricer>& Cephei::QL::Experimental::Coupons::CAveragingRatePricer::GetHandle ()
{
	if (_phAveragingRatePricer == NULL)
	{
		_phAveragingRatePricer = new Handle<QuantLib::AveragingRatePricer> (*_ppAveragingRatePricer);
	}
	return *_phAveragingRatePricer;
}
#endif
bool Cephei::QL::Experimental::Coupons::CAveragingRatePricer::HasNative () 
{
	return (_ppAveragingRatePricer != NULL);
}

Double Cephei::QL::Experimental::Coupons::CAveragingRatePricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAveragingRatePricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

